#ifdef _DEBUG

#include "d3d.h"
#include "d3d_particles.h"
#include "d3d_texture.h"
#include "d3d_mesh.h"
#include ".\shaders\d3d_shader.h"
#include ".\shaders\d3d_depthmapshader.h"
#include ".\shaders\d3d_particleshader.h"
#include ".\shaders\d3d_lightshader.h"
#include ".\shaders\d3d_screenshader.h"
#include "d3d_pointsprite.h"
#include "d3d_image.h"
#include "d3d_light.h"
#include "d3d_util.h"
#include "..\common\def.h"
#include <assert.h>


#include "..\engine\engine.h"
#include "..\input\dinput.h"

struct dbgVertex
{
    FLOAT x, y, z;
    DWORD color;
};


#define D3DFVF_DBGVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

void CD3D9Device::RenderDebug()
{
	cuint lineCount = m_dbgLines.Count();
	cuint pointCount = m_dbgPoints.Count();
	cuint sphereCount = m_dbgSpheres.Count();

	m_pD3DDevice->BeginScene();
	m_pD3DDevice->SetTransform( D3DTS_WORLD, &m_IdentityMatrix );
	m_pD3DDevice->SetTransform( D3DTS_VIEW, &m_camera.m_View);
	m_pD3DDevice->SetTransform( D3DTS_PROJECTION, &m_camera.m_Projection);
	m_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
	m_pD3DDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
	m_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
	m_pD3DDevice->SetFVF( D3DFVF_DBGVERTEX );

	if(pointCount > 0)
	{
		dbgVertex* pPointData = EE_NEW dbgVertex [ pointCount * 6 ];
		for(int i = 0; i < pointCount; i++)
		{
			DebugPoint point = m_dbgPoints[i];
			DWORD color = D3DCOLOR_COLORVALUE(point.c.r,point.c.g,point.c.b,0.0f);
			Vector3f position = point.p;
			float r = point.r;

			pPointData[(i * 6) + 0].color = color;
			pPointData[(i * 6) + 0].x = position.x - r;
			pPointData[(i * 6) + 0].y = position.y;
			pPointData[(i * 6) + 0].z = position.z;

			pPointData[(i * 6) + 1].color = color;
			pPointData[(i * 6) + 1].x = position.x + r;
			pPointData[(i * 6) + 1].y = position.y;
			pPointData[(i * 6) + 1].z = position.z;

			pPointData[(i * 6) + 2].color = color;
			pPointData[(i * 6) + 2].x = position.x;
			pPointData[(i * 6) + 2].y = position.y + r;
			pPointData[(i * 6) + 2].z = position.z;

			pPointData[(i * 6) + 3].color = color;
			pPointData[(i * 6) + 3].x = position.x;
			pPointData[(i * 6) + 3].y = position.y - r;
			pPointData[(i * 6) + 3].z = position.z;

			pPointData[(i * 6) + 4].color = color;
			pPointData[(i * 6) + 4].x = position.x;
			pPointData[(i * 6) + 4].y = position.y;
			pPointData[(i * 6) + 4].z = position.z + r;

			pPointData[(i * 6) + 5].color = color;
			pPointData[(i * 6) + 5].x = position.x;
			pPointData[(i * 6) + 5].y = position.y;
			pPointData[(i * 6) + 5].z = position.z - r;
		}


		int dataSize = sizeof(dbgVertex) * pointCount * 6;
		LPDIRECT3DVERTEXBUFFER9 pVB = NULL;
		if( SUCCEEDED (m_pD3DDevice->CreateVertexBuffer( dataSize, 0, D3DFVF_DBGVERTEX, D3DPOOL_DEFAULT, &pVB, NULL )))
		{
			dbgVertex* pVertices = NULL;
			if( SUCCEEDED( pVB->Lock( 0, dataSize, (void**)&pVertices, 0 ) ) )
			{
				memcpy( pVertices, pPointData, dataSize );
				pVB->Unlock();

				m_pD3DDevice->SetStreamSource( 0, pVB, 0, sizeof(dbgVertex) );
				m_pD3DDevice->DrawPrimitive( D3DPT_LINELIST, 0, 3 * pointCount);
				pVB->Release();
			}
		}
		EE_SAFEDELETEARRAY(pPointData);
		m_dbgPoints.ClearAllocatedSpace();
	}

	if(lineCount > 0)
	{
		dbgVertex* pLineData = EE_NEW dbgVertex [ lineCount * 2 ];
		for(int i = 0; i < lineCount; i++)
		{
			DebugLine line = m_dbgLines[i];
			DWORD color = D3DCOLOR_COLORVALUE(line.c.r,line.c.g,line.c.b,0.0f);
			pLineData[(i * 2) + 0].color = color;
			pLineData[(i * 2) + 0].x = line.v1.x;
			pLineData[(i * 2) + 0].y = line.v1.y;
			pLineData[(i * 2) + 0].z = line.v1.z;

			pLineData[(i * 2) + 1].color = color;
			pLineData[(i * 2) + 1].x = line.v2.x;
			pLineData[(i * 2) + 1].y = line.v2.y;
			pLineData[(i * 2) + 1].z = line.v2.z;
		}


		int dataSize = sizeof(dbgVertex) * lineCount * 2;
		LPDIRECT3DVERTEXBUFFER9 pVB = NULL;
		if( SUCCEEDED (m_pD3DDevice->CreateVertexBuffer( dataSize, 0, D3DFVF_DBGVERTEX, D3DPOOL_DEFAULT, &pVB, NULL )))
		{
			dbgVertex* pVertices = NULL;
			if( SUCCEEDED( pVB->Lock( 0, dataSize, (void**)&pVertices, 0 ) ) )
			{
				memcpy( pVertices, pLineData, dataSize );
				pVB->Unlock();

				m_pD3DDevice->SetStreamSource( 0, pVB, 0, sizeof(dbgVertex) );
				m_pD3DDevice->DrawPrimitive( D3DPT_LINELIST, 0, lineCount );
				pVB->Release();
			}
		}
		EE_SAFEDELETEARRAY(pLineData);
		m_dbgLines.ClearAllocatedSpace();
	}

	if(sphereCount > 0)
	{
		for(int i = 0; i < sphereCount; i++)
		{
			DebugPoint sphere = m_dbgSpheres[i];
			
			LPD3DXMESH dbgSphere = NULL;
			D3DXMATRIX t;

			D3DXMatrixTranslation(&t,sphere.p.x,sphere.p.y,sphere.p.z);
			D3DXCreateSphere(m_pD3DDevice, sphere.r, 10, 10, &dbgSphere, NULL);
	
			m_pD3DDevice->SetTransform( D3DTS_WORLD, &t);
			dbgSphere->DrawSubset(0);
			dbgSphere->Release();
		}
		m_dbgSpheres.ClearAllocatedSpace();
	}

	m_pD3DDevice->EndScene();
}

void CD3D9Device::DrawLine(const Vector3f &start, const Vector3f &end, const Color3f& color)
{
	DebugLine line = {start,end,color};
	m_dbgLines.Add(line);
}

void CD3D9Device::DrawPoint(const Vector3f &position, const float radius, const Color3f& color)
{
	DebugPoint point = {position,color,radius};
	m_dbgPoints.Add(point);
}

void CD3D9Device::DrawSphere(const Vector3f &position, const float radius, const Color3f& color)
{
	DebugPoint point = {position,color,radius};
	m_dbgSpheres.Add(point);
}

#endif